/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <semaphore.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#include "cmd_input.h"

#ifdef __cplusplus
extern "C" {
#endif

#ifndef __LITEOS_M__

//参数列表
static char **g_argv = NULL;
//参数列表总数
static int g_argc_total = 0;
//当前需要取出的参数序号
static int g_arg_pos = 0;

void ClearInputBuffer() {
    int c;
    while ((c = getchar()) != '\n' && c != EOF);
}

int32_t SleGetInputNumber(const char *info)
{
    printf("==>%s\r\n", info);
    int32_t num = -1;
    scanf("%d", &num);
    ClearInputBuffer();
    printf("num: %d", num);
    return num;
}

void SleGetInputString(const char *info, char *str, uint32_t size)
{
    printf("==>%s\r\n", info);
    if (fgets(str, size, stdin)) {
        size_t len = strlen(str);
        if (len > 0 && str[len - 1] != '\n') {
            printf("input string too long. Just get %d chars:%s\r\n", size - 1, str);
            ClearInputBuffer();
            return;
        } else if (len > 0 && str[len - 1] == '\n') {
            str[len - 1] = '\0';
        }
        printf("str: %s\r\n", str);
    } else {
        printf("error reading input.\r\n");
    }
}

void SetBufEnv(int argc, char **argv, int pos){
    g_argv = argv;
    g_argc_total = argc;
    g_arg_pos = pos;
}

void ResetEnv(void){
    g_arg_pos = 0;
    g_argc_total = 0;
    g_argv = NULL;
}

#endif //__LITEOS_M__

#ifdef __cplusplus
}
#endif
